package com.lanou3g.k12.web.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanou3g.k12.entity.Article;
import com.lanou3g.k12.service.ArticleService;
import com.lanou3g.k12.utils.K12Result;
import com.lanou3g.k12.utils.K12ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class ArticleController {

    @Autowired
    ArticleService articleService;

    // 添加资讯
    @PostMapping("/article")
    public K12Result addArticle(Long cid,String content,Integer status,String title,@RequestParam(defaultValue = "1") Integer sort){

         articleService.addArticle(cid,content,status,title,sort);

         return   K12ResultUtils.ok();

    }

    // 查询资讯
    @GetMapping("/article/{id}")
    public K12Result findArticleById(@PathVariable Long id){
        Article article = articleService.findArticleById(id);
        return K12ResultUtils.ok(article);
    }

    // 更新资讯
    @PutMapping("/article/{id}")
    public K12Result updateArticleById(@PathVariable Long id, Long cid,Integer status,String title,String content,Integer sort){

       articleService.updateArticleById(id,cid,status,title,content,sort);

       return K12ResultUtils.ok();
    }


    // 删除资讯
    @DeleteMapping("/article/{id}")
    public K12Result deleteArticleById(@PathVariable Long id){

        articleService.deleteArticleById(id);

        return K12ResultUtils.ok();
    }

    // 根据keyword查询
    @GetMapping("/articles")
    public K12Result findArticleByKeyword(String keyword,@RequestParam(value = "page",defaultValue = "1") Integer page,@RequestParam(value = "size",defaultValue = "30") Integer size){

        Page pages;
        if (keyword!=null&& !keyword.trim().equals("")){
            pages = PageHelper.startPage(page, size);
            articleService.findByKeyword(page,size,keyword);
            return K12ResultUtils.ok(pages);
        }
        else {
            pages = PageHelper.startPage(page, size);
            articleService.findAll(page,size);
            return K12ResultUtils.ok(pages);
        }

    }

    // 通过ID批量删除资讯

    @DeleteMapping("/articles/{ids}")
    public K12Result deleteArticleByIds(@PathVariable Integer[] ids){
        articleService.deleteArticleByIds(ids);
        return K12ResultUtils.ok();

    }



    // 查询资讯(后端)
    @GetMapping("/article/backend/{id}")
    public K12Result findByIdForBackend(@PathVariable Long id){
        Article article = articleService.findByIdForBackend(id);
        return K12ResultUtils.ok(article);
    }



    // 根据keyword查询(后端)
    @GetMapping("/articles/backend")
    public K12Result findByKeywordForBackend(String keyword,@RequestParam(value = "page",defaultValue = "1") Integer page,@RequestParam(value = "size",defaultValue = "30") Integer size){

        Page pages;
        if (keyword!=null&& !keyword.trim().equals("")){
            pages = PageHelper.startPage(page, size);
            articleService.findByKeywordForBackend(page,size,keyword);
            return K12ResultUtils.ok(pages);
        }
        else {
            pages = PageHelper.startPage(page, size);
            articleService.findAllForBackend(page,size);
            return K12ResultUtils.ok(pages);
        }

    }











}
